package com.springboot.fastcode.utils;


import com.springboot.fastcode.common.GenerateConstants;

/**
 *
 * 字段(表名)转换java驼峰语法工具类
 *
 * @author liaofuxing
 * @E-mail liaofuxing@outlook.com
 * @date 2020/04/09 19:56
 */
public class FieldConvertJavaHumpUtils {

    /**
     * lower case
     * 将字段转换为java驼峰风格，首字母小写 eg：xxxYyy
     *
     * @param code field字段
     * @return javaHump java驼峰字符串
     */
    public static String firstCodeLowerCaseConvertJavaHump(String code) {
        // 替换数据库中的"_"字符, 用java驼峰命名
        String javaHump;
        if (code.contains(GenerateConstants.DB_LINK_TAG)) {
            // 第一个出现的索引位置
            StringBuilder mysqlFieldBuilder = new StringBuilder(code);
            int indexOf = code.indexOf(GenerateConstants.DB_LINK_TAG);
            char c = code.charAt(indexOf + 1);
            javaHump = mysqlFieldBuilder.replace(indexOf,
                            indexOf + 2,
                            GenerateConstants.SPACE_STRING + String.valueOf(c).toUpperCase()).toString();
            while (indexOf != -1) {
                // 从这个索引往后开始第一个出现的位置
                indexOf = code.indexOf(GenerateConstants.DB_LINK_TAG, indexOf + 1);
                if (indexOf != -1) {
                    javaHump = firstCodeLowerCaseConvertJavaHump(javaHump);
                }
            }
        } else {
            javaHump = code;
        }
        return javaHump.replace(GenerateConstants.SPACE_STRING,"");
    }

    /**
     * upper case
     * 将字段转换为表名，类名风格，首字母大写 eg：XxxYyy
     *
     * @param code 需要转换的字段
     * @return tableNameBuilder.toString() 表名驼峰字符串
     *
     */
    public static String firstCodeUpperConvertJavaHump(String code) {
        String tableName = firstCodeLowerCaseConvertJavaHump(code);
        StringBuilder tableNameBuilder = new StringBuilder(tableName);
        String firstStr = tableName.substring(0, 1);
        tableNameBuilder.replace(0,1, firstStr.toUpperCase());

        return tableNameBuilder.toString();
    }
}
